from user import *
from seat import *
from poker import *
from timer import *

'''
status 
0:ready
1:start
2:preflop
3:flop
4:turn
5:river
'''

class table:
	def __init__(self, tid):
		self.tid = tid
		self.seat = []
		self.seatNum = 0
		self.looker = []
		self.sblind = 100		
		for i in range(0, 9):
			s = seat(i)
			self.seat.append(s)

		self.status = 0

		self.pokers = poker()
		self.publicPoker = []

		self.dealer = self.seat[8]
		self.nowChip = None

		self.maxRound = 0
		self.pots = []

		self.chipTimer = timer()

	def isUserInLooker(self, user):
		if user in self.looker:
			return True
		return False

	def isUserInSeat(self, user):
		for s in self.seat:
			if user == s.user:
				return True
		return False

	def isUserInTable(self, user):
		if not self.isUserInLooker() and not self.isUserInSeat():
			return True
		return False

	def login(self, user):
		if not self:
			return -1
		self.looker.append(user)
		return 0

	def logout(self, user):

		if self.isUserInLooker(user):
			self.looker.remove(user)

		if self.isUserInSeat(self):
			self.looker.remove(user)
		return 0

	def sit(self, user, seatid, sitMoney):

		if seatid < 0 or seatid > 8:
			return -1

		if not self.isUserInLooker(user):
			return -2

		if self.isUserInSeat(user):
			return -3

		if self.seat[seatid].isHaveUser():
			return -4

		self.looker.remove(user)
		self.seat[seatid].sit(user, sitMoney)
		self.seatNum = self.seatNum + 1
		print "seat num :%d" % self.seatNum
		if self.seatNum >= 2 and self.status == 0:
			self.startGame()
		return 0

	def stand(self, user):

		if not self.isUserInSeat(user):
			print "debug table stand error"
			return -2

		for i in self.seat:
			if i.user == user:
				i.stand()
		self.looker.append(user)
		self.seatNum = self.seatNum - 1
		return 0
		#seat = self.seat.find(user):
		#seat.stand();

	def chip(self, chipMoney, status):
		if status == 3: #flod
			pass
		if self.maxRound < chipMoney:
			self.maxRound = chipMoney
		self.nowChip.chip(chipMoney, status)
		self.chipTimer.stopTimer()
		if self.checkOver():
			return ;
		self.nextChipin()


	def getPlayerCnt(self):
		cnt = 0
		for i in range(0, 9):
			if self.seat[i].isInGame():
				cnt += 1
		return cnt

	def printInfo(self):
		print "table :%d" % self.tid
		for i in range(0, 9):
			s = self.seat[i]
			if s.isHaveUser() :
				print "%d," % s.user.uid,
			else:
				print "-1,",
		print ";;;looker:",
		for i in self.looker:
			print "%d" % i.uid,
		print

	

		
	def shuffle(self, keys):
		self.pokers.shuffle(keys)

	def nextDealer(self):
		next = self.dealer
		while ( True ):
			next =self.nextUser(next)
			if next.isHaveUser():
				self.dealer = next
				break
			if next == self.dealer :
				break
		return self.dealer

	def nextPlayer(self, seat):
		seatid = seat.seatid
		while(True):
			seatid = (seatid+1)%9
			s = self.seat[seatid]
			if s.isInGame():
				return s
			if s == seat:
				return seat

	def nextUser(self, seat):
		seatid = seat.seatid
		while(True):
			seatid = (seatid+1)%9
			s = self.seat[seatid]
			if s.isHaveUser():
				return s
			if s == seat:
				return seat

	def dealPotInfo():
		pass

	def overGame(self):
		flodPots = 0
		normalList = []
		for s in self.seat:
			if s.status == 3:
				flodPots += s.allChip
			if s.status == 2:
				normalList.append(s)

		#print "#######"
		#for i in normalList:
		#	print "%d, " % i.allChip
		#print "\n#######"
		#sort(normalList)
		pots = []
		num = len(normalList)
		highLevel = 0
		lastPot = 0
		index = 0
		for s in normalList:
			if lastPot != s.allChip:
				pots.append((s.allChip-highLevel)*num)
				highLevel = s.allChip
				lastPot = s.allChip
				index += 1
			num -= 1
		pots[0] += flodPots
		#print "flod money :%d" % flodPots
		#print pots
		print "==over now money:"
		for s in self.seat:
			if s.isHaveUser():
				print "%d" % s.money,
			else:
				print "0",
		print 
		pass

	def startGame(self):
		self.status = 1
		self.nextDealer()
		#print "dealer id :%d" % self.dealer.seatid

		self.shuffle(10)
		print "GameStart"
		for s in self.seat:
			if s.isHaveUser():
				s.start()
				s.setHandle(self.pokers.popPoker(), self.pokers.popPoker())

		print "public:",
		for i in range(0, 5):
			public = self.pokers.popPoker()
			self.publicPoker.append(public)
			print "%x," % public, 
		print

		#small blind
		sb = self.nextPlayer(self.dealer)
		sb.systemChip(self.sblind)

		bb = self.nextPlayer(sb)
		bb.systemChip(self.sblind*2)
		self.maxRound = self.sblind*2

		self.nowChip = bb;
		self.nextChipin()

	def checkOver(self):
		players = self.getPlayerCnt()
		print "players :%s" % players
		if players <= 1:
			print "game over"
			self.overGame()
			return True
		return False
		
	def chipinTimeout(self):
		print "chipinTimerout"
		chipUser = self.nowChip.user
		print "system flod seatid:%d" % self.nowChip.seatid
		chipUser.userChip(0, 3) #flod
		#self.nextChipin()
		

	def calcChpinInfo(self):
		#print self.maxRound, self.nowChip.allChip
		check = self.maxRound - self.nowChip.allChip
		min = check+self.sblind*2
		max = self.nowChip.money
		print "##########req##############check:%d, min:%d, max:%d" % (check, min, max)
		pass

	def nextChipin(self):
		req = self.nextPlayer(self.nowChip)
		print "nextChipin id :%d" % req.seatid
		self.nowChip = req;
		req.setChipFlag()
		self.calcChpinInfo()

		print "@@@@@@@start Timer test "
		self.chipTimer.startTimer(5, self.chipinTimeout);


tables = {}




